{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "280b3aa9",
   "metadata": {},
   "source": [
    "## 数组的广播机制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "5e85f266",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "04d1a949",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[7 2 8 4 5 6]\n",
      " [4 3 0 4 4 5]\n",
      " [1 6 0 5 7 2]\n",
      " [2 2 3 7 2 2]]\n"
     ]
    }
   ],
   "source": [
    "a1 = np.random.randint(0,9,size=(4,6))\n",
    "print(a1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "fdfab2c6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[14  4 16  8 10 12]\n",
      " [ 8  6  0  8  8 10]\n",
      " [ 2 12  0 10 14  4]\n",
      " [ 4  4  6 14  4  4]]\n"
     ]
    }
   ],
   "source": [
    "#数组直接乘以某数可以让所有元素都乘以某数\n",
    "a2 = a1*2\n",
    "print(a2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "f55ee394",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 7 7 6 5 8]\n",
      " [7 6 7 0 5 0]\n",
      " [6 5 4 3 7 5]\n",
      " [7 4 1 0 2 6]]\n"
     ]
    }
   ],
   "source": [
    "a3 = np.random.randint(0,9,size=(4,6))\n",
    "print(a3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "8e54e202",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 8,  9, 15, 10, 10, 14],\n",
       "       [11,  9,  7,  4,  9,  5],\n",
       "       [ 7, 11,  4,  8, 14,  7],\n",
       "       [ 9,  6,  4,  7,  4,  8]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#形状一致可以直接参与运算\n",
    "a1+a3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "66fdf015",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3, 1, 4, 0],\n",
       "       [2, 1, 4, 2],\n",
       "       [1, 2, 4, 0]])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a4=np.random.randint(0,5,size=(3,4))\n",
    "a4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "c064f72b",
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "operands could not be broadcast together with shapes (4,6) (3,4) ",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[0;32m/tmp/ipykernel_21507/711844814.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0;31m#形状不一致不能直接相加运算。但行数相同，列数不同的可以运算\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0ma1\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0ma4\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m: operands could not be broadcast together with shapes (4,6) (3,4) "
     ]
    }
   ],
   "source": [
    "#形状不一致不能直接相加运算。\n",
    "a1+a4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "72229db0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4],\n",
       "       [4],\n",
       "       [3]])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#但行数相同，列数不同的可以运算。每行的一个数据加到每一行的各个数字上。限于只有一列。\n",
    "a5=np.random.randint(0,5,size=(3,1))\n",
    "a5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "bfaff756",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[7, 5, 8, 4],\n",
       "       [6, 5, 8, 6],\n",
       "       [4, 5, 7, 3]])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a4+a5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "50522520",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2, 3, 2, 4]])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#列数相同，行数不同的化，单一行的数据加(乘)到每一行上。限于一行。\n",
    "a6 = np.random.randint(0,5,size=(1,4))\n",
    "a6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "c8bbfd20",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[6, 3, 8, 0],\n",
       "       [4, 3, 8, 8],\n",
       "       [2, 6, 8, 0]])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a4*a6"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "28b62425",
   "metadata": {},
   "source": [
    "### 三维数组也可以相加，需要符合广播原则"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4fe3476b",
   "metadata": {},
   "source": [
    "## 广播的总结\n",
    "1. 数组直接和数字广播运算没有问题。\n",
    "2. 两个shape相同的数组可以直接运算。(包括加减乘除)\n",
    "3. 两个shape不同的数组是否可以直接运算，取决于是否满足广播原则。\n",
    "4. 广播的原则:如果两个数组的后缘维度(即从末尾开始算起的维度)的轴长度相符或其中一方的长度为1，则认为他们的广播是兼容的。\n",
    "    广播会在缺失和（或）长度为1的维度上进行。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
